﻿namespace TowerDefense.ToolKit
{
    using System;
    using System.Collections.Generic;
    using System.IO;

    using Microsoft.Xna.Framework;

    public class Parser
    {
        /// <summary>
        /// Parses a Map textfile
        /// </summary>
        /// <param name="mapFile">Textfile to be parsed</param>
        /// <returns>Map object with data from parse</returns>
        public static Map ParseMapFile(string mapFile)

        {
            MapRoute mRoute = new MapRoute();
            string line = string.Empty;
            Area[,] parsedArea;
            List<string> rows = new List<string>();
            Vector2 startPos = new Vector2();
            Vector2 endPos = new Vector2();

            if (!File.Exists(@mapFile))
            {
                throw new FileNotFoundException("File: " + mapFile + " not found!");
            }

            int i = 0;
            using (FileStream stream = new FileStream(@mapFile, FileMode.Open, FileAccess.Read))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    while ((line = reader.ReadLine()) != null)
                    {
                        rows.Add(line);
                        i++;
                    }
                }
            }

            int rowLength = rows[0].Length;

            if (rowLength > 0)
            {
                parsedArea = new Area[i, rowLength];
            }
            else
            {
                throw new Exception("Error parsing MapFile: Badly formated line.");
            }

            Dictionary<int, Vector2> di = new Dictionary<int, Vector2>();

            for (int y = 0; y < i; y++)
            {
                for (int x = 0; x < rowLength; x++)
                {
                    // Parse one char at a time.
                    Char c = (Char)rows[y][x];

                    if (Char.IsDigit(c))
                    {
                        mRoute.Waypoints.Add(new Waypoint(int.Parse(c.ToString()),new Vector2(x * Map.AREA_SIZE + Map.AREA_SIZE / 2, y * Map.AREA_SIZE + Map.AREA_SIZE / 2)));
                        parsedArea[x, y] = new Area(TDGame.textures["road"], false);
                    }
                    else
                    {
                        switch (c)
                        {
                            case 'X':
                                parsedArea[x,y] = new Area(TDGame.textures["gras"], true);
                                break;

                            /* Road */
                            case '.':
                                parsedArea[x, y] = new Area(TDGame.textures["road"], false);
                                break;

                            /* End Point */
                            case 'e':
                                parsedArea[x, y] = new Area(TDGame.textures["road"], false);
                                endPos = new Vector2(x * Map.AREA_SIZE + Map.AREA_SIZE / 2, y * Map.AREA_SIZE + Map.AREA_SIZE / 2);
                                break;

                            /* Start Point */
                            case 's':
                                parsedArea[x, y] = new Area(TDGame.textures["road"], false);
                                startPos = new Vector2(x * Map.AREA_SIZE + Map.AREA_SIZE / 2, y * Map.AREA_SIZE + Map.AREA_SIZE / 2);
                                break;

                            /* Default, should maybe be buildable? */
                            default:
                                /* Road */
                                parsedArea[x, y] = new Area(TDGame.textures["road"], false);
                                break;
                        }
                    }
                }
            }

            return new Map(mRoute, new List<Level>(), startPos, endPos , parsedArea);
        }
    }
}
